package com.umfwechat.lightprogram.controller.my;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.umfwechat.common.code.RetCode;
import com.umfwechat.common.constant.CommonConsts;
import com.umfwechat.common.prop.UmfPropModel;
import com.umfwechat.util.ReqMessageUtil;

/**
 * 业务员重置密码；
 * @author llb
 *
 */
@Controller
@RequestMapping("/agentManagement")
public class SalesmanManagementController {
	private static Logger logger = LoggerFactory.getLogger(SalesmanManagementController.class);
	
	/**
	 * 业务员重置密码；
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/resetSalesmanPwd")
	@ResponseBody
	public JSONObject resetPassword(HttpServletRequest request, HttpServletResponse response) throws Exception{	
		JSONObject json= new JSONObject();
		json.put(CommonConsts.RETCODE, RetCode.SUCCESS);
		json.put(CommonConsts.RETMSG, "成功");
		//获取请求参数
		String reqJSON=  (String) request.getAttribute(CommonConsts.REQUESTDATA);
		JSONObject param=JSONObject.fromObject(reqJSON);
		String agentId = param.optString("loginAgentId");	//代理商编号
		String assistId = param.optString("loginAssistId");		//代理商的账号assistId:id:b:ceshia
		String userName = param.optString("userId");	//用户名:userId:admin
		Map<String, Object> req = ReqMessageUtil.makeReqCrmMap();
		req.put("id", assistId);//账号
		req.put(CommonConsts.AGENTID, agentId);//代理商编号
		req.put(CommonConsts.USERNAME, userName);
		req.put("managerType", "1");//管理员类别,0-admin 1-普通操作员
		Map<String,Object> res = null;
		try {
			logger.info("【调用CRM重置登录密码接口】入参："+req);
			XStream xstream = new XStream(new DomDriver());
			String url=UmfPropModel.INSTANCE.getPropValue("agent.crm.resetAgentManagerLoginPassword");
			res = (Map<String, Object>) com.umfwechat.util.HttpUtil.getResObjPost(url, xstream, req);
			logger.info("【调用CRM重置登录密码接口】出参："+res);
		} catch (Exception e) {
			json.put(CommonConsts.RETCODE, "0001");
			json.put(CommonConsts.RETMSG, "调用crm重置登录密码接口异常");
			logger.info("【调用crm重置登录密码接口异常】");
			return json;
		}
		if(res==null){
			json.put(CommonConsts.RETCODE, "00434101");
			json.put(CommonConsts.RETMSG, "服务器异常，请稍后再试");
			return json;
		}
		String retCode=(String)res.get("retCode");
		if(!"0000".equals(retCode)){
			json.put(CommonConsts.RETCODE, "00434101");
			json.put(CommonConsts.RETMSG, "服务器异常，请稍后再试");
			return json;
		}
		return json;
	}
	/**
	 * 重置代理商登陆密码；
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/resetAgentPwd")
	@ResponseBody
	public JSONObject resetAgentPwd(HttpServletRequest request, HttpServletResponse response) throws Exception{	
		JSONObject json= new JSONObject();
		json.put(CommonConsts.RETCODE, RetCode.SUCCESS);
		json.put(CommonConsts.RETMSG, "成功");
		//获取请求参数
		String reqJSON=  (String) request.getAttribute(CommonConsts.REQUESTDATA);
		JSONObject param=JSONObject.fromObject(reqJSON);
		String agentId = param.optString("agentId");	//代理商编号
		String assistId = param.optString("assistId");		//代理商的账号assistId:id:b:ceshia
		String userName = param.optString("userId","admin");	//用户名:userId:admin
		Map<String, Object> req = ReqMessageUtil.makeReqCrmMap();
		req.put("id", assistId);//账号
		req.put(CommonConsts.AGENTID, agentId);//代理商编号
		req.put(CommonConsts.USERNAME, userName);
		if("admin".equals(userName)){
			req.put("managerType", "0");//管理员类别,0-admin 1-普通操作员
		}else{
			req.put("managerType", "1");//管理员类别,0-admin 1-普通操作员
		}
		Map<String,Object> res = null;
		try {
			logger.info("【调用CRM重置登录密码接口】入参："+req);
			XStream xstream = new XStream(new DomDriver());
			String url=UmfPropModel.INSTANCE.getPropValue("agent.crm.resetAgentManagerLoginPassword");
			res = (Map<String, Object>) com.umfwechat.util.HttpUtil.getResObjPost(url, xstream, req);
			logger.info("【调用CRM重置登录密码接口】出参："+res);
		} catch (Exception e) {
			json.put(CommonConsts.RETCODE, "0001");
			json.put(CommonConsts.RETMSG, "调用crm重置登录密码接口异常");
			logger.info("【调用crm重置登录密码接口异常】");
			return json;
		}
		if(res==null){
			json.put(CommonConsts.RETCODE, "00434101");
			json.put(CommonConsts.RETMSG, "服务器异常，请稍后再试");
			return json;
		}
		String retCode=(String)res.get("retCode");
		if(!"0000".equals(retCode)){
			json.put(CommonConsts.RETCODE, "00434101");
			json.put(CommonConsts.RETMSG, "服务器异常，请稍后再试");
			return json;
		}
		return json;
	}
}
